package com.jiaqiang.chatgpt.session.defaults;

import com.jiaqiang.chatgpt.IOpenAiApi;
import com.jiaqiang.chatgpt.domain.billing.BillingUsage;
import com.jiaqiang.chatgpt.domain.billing.CreditSummary;
import com.jiaqiang.chatgpt.domain.billing.Subscription;
import com.jiaqiang.chatgpt.session.Configuration;
import com.jiaqiang.chatgpt.session.UserOpenAiSession;
import okhttp3.sse.EventSource;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2024-03-28
 * Time: 14:29
 */
public class DefaultUserSessionOpenAiSession implements UserOpenAiSession {



    /** 配置信息 */
    private final Configuration configuration;
    /** 工厂事件 */
    private final EventSource.Factory factory;
    /** openai 接口 */
    private final IOpenAiApi openAiApi;

    public DefaultUserSessionOpenAiSession(Configuration configuration) {
        this.configuration = configuration;
        this.openAiApi = configuration.getOpenAiApi();
        this.factory = configuration.createRequestFactory();
    }

    /**
     * 账单查询
     *
     * @return 应答结果
     */
    @Override
    public Subscription subscription(String authorization) {
        return this.openAiApi.subscription(authorization).blockingGet();
    }

    /**
     * 消耗查询
     *
     * @param starDate 开始时间
     * @param endDate  结束时间
     * @return  应答数据
     */
    @Override
    public BillingUsage billingUsage(@NotNull LocalDate starDate, @NotNull LocalDate endDate) {
        return this.openAiApi.billingUsage(starDate, endDate).blockingGet();
    }

    /**
     * 余额查询 需要 session key
     * @return 应答数据
     */
    @Override
    public CreditSummary billingCreditGrants() {
        return  this.openAiApi.billingCreditGrants().blockingGet();
    }

}
